<template>
    <div style="margin-left: 30px;margin-top: 15px;">
        <!-- 查询条件 -->
        <el-form :inline="true" :model="search" class="demo-form-inline">
            <el-form-item label="配种日期">
                <el-date-picker v-model="search.BreedingDate" type="datetimerange" range-separator="-"
                    start-placeholder="Start date" end-placeholder="End date" value-format="YYYY-MM-DD"
                    style="width: 310px" />
            </el-form-item>
            <el-form-item label="种母耳号">
                <el-input v-model="search.Earbugles" clearable />
            </el-form-item>

            <el-form-item label="负责人">
                <el-select v-model="search.Head" clearable style="width: 123px;">
                    <el-option :label="item.name" :value="item.name" v-for="item in HeadList" :key="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" @click="GetBreedingList" :icon="Search">查询</el-button>
            </el-form-item>

        </el-form>

        <div style="margin-top: 9px;">
            <el-button type="danger" size="default" :icon="Plus" @click="OpenDialog">新增</el-button>
            <el-button type="danger" size="default" :icon="Delete" @click="DelAll">删除</el-button>
        </div>

        <!-- 列表 -->
        <div style="margin-top: 25px;">
            <el-table :data="BreedingList" stripe style="width: 85%" @selection-change="GetBreedingIds">
                <el-table-column type="selection" width="35" fixed align="center" />
                <el-table-column prop="name" label="序号" align="center" fixed>
                    <template #default="a">
                        <span>{{ a.$index + (page.pageIndex - 1) * page.pageSize + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="breedingDate" label="配种日期" align="center" width="130">
                    <template v-slot="a">
                        <span v-if="a.row.breedingDate">{{ a.row.breedingDate.substring(0, 10) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="lastDate" label="上一次配种日期" align="center" width="130">
                    <template v-slot="a">
                        <span v-if="a.row.lastDate">{{ a.row.lastDate.substring(0, 10) }}</span>
                        <span v-else>----</span>
                    </template>
                </el-table-column>
                <el-table-column prop="earbugles" label="耳号" align="center" width="130" />
                <el-table-column prop="breedinghenType" label="种母品种" align="center" width="130" />
                <el-table-column prop="serviceRam" label="配种公羊" align="center" width="130" />
                <el-table-column prop="seedmaleType" label="种公品种" align="center" width="130" />
                <el-table-column prop="hybridizationType" label="配种方式" align="center" width="130" />
                <el-table-column prop="speciesEstrusType" label="种母发情类型" align="center" width="130" />
                <el-table-column prop="breedingScore" label="配种评分" align="center" />
                <el-table-column prop="currentBuilding" label="当前栋舍" align="center" width="130px" />
                <el-table-column prop="currentField" label="当前栏位" align="center" width="130px" />
                <el-table-column prop="originallyDorm" label="转入栋舍" align="center" width="130px" />
                <el-table-column prop="originallyColumns" label="转入栏位" align="center" width="130px" />
                <el-table-column prop="reproductiveState" label="繁殖状态" align="center" width="130px" />
                <el-table-column prop="eventDate" label="事件日期" align="center" width="130px">
                    <template v-slot="a">
                        <span v-if="a.row.eventDate">{{ a.row.eventDate.substring(0, 10) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="head" label="负责人" align="center" width="130px" />
                <el-table-column prop="createBy" label="创建人" align="center" width="130px" />
                <el-table-column prop="createDate" label="创建时间" align="center" width="130px">
                    <template v-slot="a">
                        <span v-if="a.row.createDate">{{ a.row.createDate.substring(0, 10) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="updateBy" label="修改人" align="center" width="130px" />
                <el-table-column prop="updateDate" label="修改时间" align="center" width="130px">
                    <template v-slot="a">
                        <span v-if="a.row.updateDate">{{ a.row.updateDate.substring(0, 10) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" fixed="right" width="130px">
                    <template v-slot="a">
                        <el-link type="primary" style="margin-right: 15px;" @click="OpenUpd(a.row.breedId)">编辑</el-link>
                        <el-link type="primary" @click="BreedingDel(a.row.breedId)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 分页 -->
        <table style="margin: auto;margin-left: 300px;margin-top: 25px;">
            <tbody>
                <tr>
                    <td>
                        <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                            :page-sizes="[15, 20, 35, 50]" :background="true" layout="total, slot,sizes"
                            :total="page.totalCount" @size-change="handleSizeChange"
                            @current-change="handleCurrentChange" prev-text="上一页" next-text="下一页">
                            <span>，共{{ page.pageCount }}页，当前第{{ page.pageIndex }}/{{
                                page.pageCount
                                }}页</span>
                        </el-pagination>
                    </td>
                    <td>
                        <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                            :page-sizes="[15, 20, 35, 50]" :background="true" layout="slot, prev, pager, next"
                            :total="page.totalCount" @size-change="handleSizeChange"
                            @current-change="handleCurrentChange" prev-text="上一页" next-text="下一页">
                            <button class="btn-prev" @click="handleCurrentChange(1)" :disabled="page.pageIndex == 1">
                                首页
                            </button>
                        </el-pagination>
                    </td>
                    <td>
                        <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                            :page-sizes="[15, 20, 35, 50]" :background="true" layout="slot, jumper"
                            :total="page.totalCount" @size-change="handleSizeChange"
                            @current-change="handleCurrentChange" prev-text="上一页" next-text="下一页">
                            <button class="btn-next" @click="handleCurrentChange(page.pageCount)"
                                :disabled="page.pageIndex == page.pageCount">
                                尾页
                            </button>
                        </el-pagination>
                    </td>
                </tr>
            </tbody>
        </table>


        <!-- 弹出层添加 -->
        <el-dialog v-model="dialogVisible" title="添加配种信息" width="1300" style="height: 600px;">
            <div class="dialog-footer">
                <div style="margin-left: 25px;margin-top: 15px">
                    <div>
                        <el-button type="danger" size="default" :icon="Plus" v-show="BreedingCreate.breedId == 0"
                            @click="AddRow">新增</el-button>
                        <!-- 弹出层添加 -->
                        <el-dialog v-model="dialogVisible" title="添加配种信息" width="1300" style="height: 600px;">
                            <span>
                                <div class="dialog-footer">
                                    <div style="margin-left: 25px;margin-top: 15px">
                                        <div>
                                            <el-button type="danger" size="default" :icon="Plus"
                                                v-show="BreedingCreate.breedId == 0" @click="AddRow">新增</el-button>

                                            <el-button type="danger" size="default" :icon="Document"
                                                v-show="BreedingCreate.breedId == 0" @click="BreedingAdd">保存</el-button>
                                            <el-button type="danger" size="default" :icon="Document"
                                                v-show="BreedingCreate.breedId != 0" @click="Upd">修改</el-button>
                                            <el-button type="danger" size="default" :icon="Document"
                                                v-show="BreedingCreate.breedId != 0" @click="Upd">修改</el-button>
                                        </div>
                                        <div style="margin-top: 25px;margin-left: 25px">
                                            <span>
                                                <el-form :inline="true" :model="BreedingCreate"
                                                    class="demo-form-inline">
                                                    <el-form-item label="*负责人">
                                                        <el-select v-model="BreedingCreate.head" clearable
                                                            style="width: 123px;">
                                                            <el-option :label="item.name" :value="item.name"
                                                                v-for="item in HeadList" :key="item.id" />
                                                        </el-select>
                                                    </el-form-item>
                                                    <el-form-item label="*配种日期">
                                                        <el-date-picker v-model="BreedingCreate.breedingDate"
                                                            type="date" placeholder="请选择" clearable
                                                            style="width: 180px;" value-format="YYYY-MM-DD" />
                                                    </el-form-item>
                                                </el-form>
                                            </span>
                                        </div>
                                        <div style="margin-top: 35px;">
                                            <el-table style="width: 90%" :data="RowshiftList">
                                                <el-table-column label="序号" align="center" fixed>
                                                    <template #default="a">
                                                        {{ a.$index + 1 }}
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="*耳号" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.earbugles" filterable
                                                                placeholder="请输入" style="width: 110px"
                                                                @change="GetSheepInfo">
                                                                <el-option v-for="item in ZMEarList" :key="item.name"
                                                                    :label="item.name" :value="item.name" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="*配种公羊" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.serviceRam" filterable
                                                                placeholder="请选择" style="width: 110px">
                                                                <el-option v-for="item in ZGEarList" :key="item.name"
                                                                    :label="item.name" :value="item.name" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="*配种方式" align="center" width="130px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.hybridizationType"
                                                                clearable style="width: 110px;">
                                                                <el-option label="鲜精-阴道" value="鲜精-阴道" />
                                                                <el-option label="鲜精-子宫角" value="鲜精-子宫角" />
                                                                <el-option label="冻精-阴道" value="冻精-阴道" />
                                                                <el-option label="冻精-子宫角" value="冻精-子宫角" />
                                                                <el-option label="模糊本交" value="模糊本交" />
                                                                <el-option label="精准本交" value="精准本交" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="*种母发情类型" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <el-select v-model="BreedingCreate.speciesEstrusType" clearable
                                                            style="width: 110px;">
                                                            <el-option label="自然发情" value="自然发情" />
                                                            <el-option label="同期发情" value="同期发情" />
                                                        </el-select>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="配种评分" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-input v-model="BreedingCreate.breedingScore"
                                                                style="width: 110px" />
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="当前栋舍" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-input v-model="BreedingCreate.currentBuilding"
                                                                style="width: 110px" readonly />
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="当前栏位" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-input v-model="BreedingCreate.currentField"
                                                                style="width: 110px" readonly />
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="转入栋舍" align="center" width="135">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.originallyDorm" clearable
                                                                style="width: 120px;" @change="GetCreateInColumnList">
                                                                <el-option :label="item.Name" :value="item.Name"
                                                                    v-for="item in CreateInDormList" :key="item.Id" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="转入栏位" align="center" width="135">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.originallyColumns"
                                                                clearable style="width: 120px;">
                                                                <el-option :label="item.Name" :value="item.Name"
                                                                    v-for="item in CreateInColumnList" :key="item.Id" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="繁殖状态" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-select v-model="BreedingCreate.reproductiveState"
                                                                clearable style="width: 110px;">
                                                                <el-option label="后备空怀" value="后备空怀" />
                                                                <el-option label="空怀" value="空怀" />
                                                                <el-option label="返情空怀" value="返情空怀" />
                                                                <el-option label="流产空怀" value="流产空怀" />
                                                                <el-option label="空胎空怀" value="空胎空怀" />
                                                                <el-option label="断奶空怀" value="断奶空怀" />
                                                            </el-select>
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="事件日期" align="center" width="143px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-date-picker v-model="BreedingCreate.eventDate"
                                                                type="date" placeholder="请选择" style="width: 100%"
                                                                value-format="YYYY-MM-DD" />
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                                <el-table-column label="重量(公斤)" align="center" width="123px">
                                                    <template v-slot="a">
                                                        <span v-if="activeIndex == a.$index">
                                                            <el-input v-model="BreedingCreate.weight"
                                                                style="width: 110px" />
                                                        </span>
                                                    </template>
                                                </el-table-column>
                                            </el-table>
                                        </div>
                                    </div>
                                </div>
                            </span>
                        </el-dialog>
                        <el-button type="danger" size="default" :icon="Back" @click="">撤销</el-button>
                    </div>
                    <div style="margin-top: 25px;margin-left: 25px">
                        <span>
                            <el-form :inline="true" :model="BreedingCreate" class="demo-form-inline">
                                <el-form-item label="负责人">
                                    <el-input v-model="BreedingCreate.head" placeholder="请输入" clearable
                                        style="width: 180px;" />
                                </el-form-item>
                                <el-form-item label="配种日期">
                                    <el-date-picker v-model="BreedingCreate.breedingDate" type="date" placeholder="请选择"
                                        clearable style="width: 180px;" />
                                </el-form-item>
                            </el-form>
                        </span>
                    </div>
                    <div style="margin-top: 35px;">
                        <el-table style="width: 90%" :data="RowshiftList">
                            <el-table-column label="序号" align="center" fixed>
                                <template #default="a">
                                    {{ a.$index + 1 }}
                                </template>
                            </el-table-column>
                            <el-table-column label="耳号" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.earbugles" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="配种公羊" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.serviceRam" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="配种方式" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-select v-model="BreedingCreate.hybridizationType" clearable
                                            style="width: 110px;">
                                            <el-option label="鲜精-阴道" value="鲜精-阴道" />
                                            <el-option label="鲜精-子宫角" value="鲜精-子宫角" />
                                            <el-option label="冻精-阴道" value="冻精-阴道" />
                                            <el-option label="冻精-子宫角" value="冻精-子宫角" />
                                            <el-option label="模糊本交" value="模糊本交" />
                                            <el-option label="精准本交" value="精准本交" />
                                        </el-select>
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="种母发情类型" align="center" width="123px">
                                <template v-slot="a">
                                    <el-select v-model="BreedingCreate.speciesEstrusType" clearable
                                        style="width: 110px;">
                                        <el-option label="自然发情" value="自然发情" />
                                        <el-option label="同期发情" value="同期发情" />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="配种评分" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.breedingScore" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="当前栋舍" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.currentBuilding" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="当前栏位" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.currentField" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="转入栋舍" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.originallyDorm" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="转入栏位" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.originallyColumns" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="繁殖状态" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-select v-model="BreedingCreate.reproductiveState" clearable
                                            style="width: 110px;">
                                            <el-option label="后备空怀" value="后备空怀" />
                                            <el-option label="空怀" value="空怀" />
                                            <el-option label="返情空怀" value="返情空怀" />
                                            <el-option label="流产空怀" value="流产空怀" />
                                            <el-option label="空胎空怀" value="空胎空怀" />
                                            <el-option label="断奶空怀" value="断奶空怀" />
                                        </el-select>
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="事件日期" align="center" width="143px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-date-picker v-model="BreedingCreate.eventDate" type="date" placeholder="请选择"
                                            style="width: 100%" value-format="YYYY-MM-DD" />
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="重量(公斤)" align="center" width="123px">
                                <template v-slot="a">
                                    <span v-if="activeIndex == a.$index">
                                        <el-input v-model="BreedingCreate.weight" style="width: 110px" />
                                    </span>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>




<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import {
    Plus, Document, Delete, Back, Search
} from '@element-plus/icons-vue'
import http from '@/http'



interface selectList {
    id: "",
    name: ""
}

//获取负责人信息
const HeadList = ref<selectList[]>([]);
const GetHeadList = () => {
    http.get("Readroute/api/StaffInfo/GetStaff").then(x => {
        HeadList.value = x.data;
    })
}

//添加用转入栋舍
const CreateInDormList = ref<{
    Id: "",
    Name: ""
}[]>([])
const GetCreateInDormList = () => {
    http.get("OrganizationRead/api/Cottage/HandleGetCot").then(x => {
        CreateInDormList.value = x.data;
    })
}
const CreateInColumnList = ref<{
    Id: "",
    Name: ""
}[]>([])
const GetCreateInColumnList = () => {
    if (BreedingCreate.originallyDorm) {
        http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + BreedingCreate.originallyDorm).then(x => {
            CreateInColumnList.value = x.data;
        })
    }
}

//获取耳标下拉框
const EarNumberList = ref<{
    id: "",
    name: ""
}[]>([]);
const GetEarNumber = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionEard").then(x => {
        EarNumberList.value = x.data;
    })
}
//根据耳号获取羊只档案信息
const GetSheepInfo = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionFinds?card=" + BreedingCreate.earbugles).then(x => {
        BreedingCreate.currentBuilding = x.data.cottage;
        BreedingCreate.currentField = x.data.field;
    })
}


//批量删除
var BreedingIds = ref("");
const GetBreedingIds = (ids: any) => {
    for (let index = 0; index < ids.length; index++) {
        BreedingIds.value += ids[index].breedId + ",";
    }
}

const AllOronly = ref(false);
const DelAll = () => {
    AllOronly.value = true;
    BreedingDel(BreedingIds.value.substring(0, BreedingIds.value.length - 1));
}
//删除配种信息
const BreedingDel = (id: any) => {
    ElMessageBox.confirm(
        '确定要删除吗?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'error',
        }
    )
        .then(() => {
            http.delete("Kroute/api/Breedings/BreedingDelete?Ids=" + id).then(x => {
                if (x.data > 0) {
                    ElMessage.success("删除成功")

                    if (AllOronly.value == true) {
                        AllOronly.value = false;
                        var ids = id.split(",");
                        ids.forEach((a: any) => {
                            http.get("RouteRead/api/Breedings/GetBreedingInfo?Id=" + a).then(x => {
                                SheepUpd.earNumber = x.data.earbugles;
                                SheepUpd.cottage = x.data.currentBuilding;
                                SheepUpd.field = x.data.currentField;
                                http.put("Mroute/api/SheepFile/SheepFileConditionAuricleUpdate", SheepUpd)
                            })
                        });
                    } else {
                        http.get("RouteRead/api/Breedings/GetBreedingInfo?Id=" + id).then(x => {
                            SheepUpd.earNumber = x.data.earbugles;
                            SheepUpd.cottage = x.data.currentBuilding;
                            SheepUpd.field = x.data.currentField;
                            http.put("Mroute/api/SheepFile/SheepFileConditionAuricleUpdate", SheepUpd)
                        })
                    }


                    GetBreedingList();
                } else {
                    ElMessage.error("删除失败")
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消操作',
            })
        })
}



//修改配种信息
const OpenUpd = (id: any) => {
    RowshiftList.value = [];
    dialogVisible.value = true;
    http.get("RouteRead/api/Breedings/GetBreedingInfo?Id=" + id).then(x => {
        var res = x.data;
        AddRow();
        BreedingCreate.breedId = res.breedId;
        BreedingCreate.earbugles = res.earbugles;
        BreedingCreate.serviceRam = res.serviceRam;
        BreedingCreate.hybridizationType = res.hybridizationType;
        BreedingCreate.speciesEstrusType = res.speciesEstrusType;
        BreedingCreate.breedingScore = res.breedingScore;
        BreedingCreate.currentBuilding = res.currentBuilding;
        BreedingCreate.currentField = res.currentField;
        BreedingCreate.originallyDorm = res.originallyDorm;
        BreedingCreate.originallyColumns = res.originallyColumns;
        BreedingCreate.reproductiveState = res.reproductiveState;
        BreedingCreate.eventDate = res.eventDate;
        BreedingCreate.weight = res.weight;
        BreedingCreate.head = res.head;
        BreedingCreate.breedingDate = res.breedingDate;
        BreedingCreate.createBy = res.createBy;
        BreedingCreate.createDate = res.createDate;
        BreedingCreate.updateBy = res.updateBy;
        BreedingCreate.updateDate = res.updateDate;
        BreedingCreate.isDel = res.isDel;
    })
}
const Upd = () => {
    http.get("RouteRead/api/Historys/HistoryEarsel?EarNumber=" + BreedingCreate.earbugles).then(res => {
        var info = res.data;


        //更改羊只栋舍信息
        SheepUpd.earNumber = BreedingCreate.earbugles;
        SheepUpd.cottage = BreedingCreate.originallyDorm;
        SheepUpd.field = BreedingCreate.originallyColumns;
        http.put("Mroute/api/SheepFile/SheepFileConditionAuricleUpdate", SheepUpd)

        http.get("RouteRead/api/Historys/HistoryEarsel?EarNumber=" + BreedingCreate.earbugles).then(res => {
            var info = res.data;

            http.put("Kroute/api/Breedings/BreedingUpdate", BreedingCreate).then(x => {
                if (x.data > 0) {
                    ElMessage.success("修改成功")
                    dialogVisible.value = false;
                    GetBreedingList();

                    //添加种母历史记录
                    HistoryInfo.earbugles = BreedingCreate.earbugles;//种母耳号
                    HistoryInfo.breedingDate = BreedingCreate.breedingDate;//配种日期
                    HistoryInfo.seedMaleEarbugles = BreedingCreate.serviceRam;//种公耳号
                    HistoryInfo.atPresentParity = info.atPresentParity + 1;//当前胎次
                    http.post("Kroute/api/Historys/HistoryCreate", HistoryInfo);

                    //更改档案的妊娠状态为已妊娠
                    http.get("MYroute/api/SheepFile/SheepFileConditionFinds?card=" + BreedingCreate.earbugles).then(res => {
                        var form = res.data;
                        form.reproductiveState = 7;
                        http.put("Mroute/api/SheepFile/SheepFileConditionUpdate", form)
                    })

                } else {
                    ElMessage.error("修改失败")
                }
            })
        })
    })
}

//新增行操作
var activeIndex = ref(-1);
var RowshiftList = ref<{}[]>([])
const AddRow = () => {
    var newrow = {
        "earbugles": null,
        "serviceRam": null,
        "hybridizationType": null,
        "speciesEstrusType": null,
        "breedingScore": null,
        "currentBuilding": null,
        "currentField": null,
        "originallyDorm": null,
        "originallyColumns": null,
        "reproductiveState": null,
        "eventDate": null,
        "weight": null,
        "head": null,
        "breedingDate": null
    }
    RowshiftList.value.push(newrow)
    activeIndex.value = RowshiftList.value.length - 1;
}

//新增配种信息
//打开新增弹出层
const dialogVisible = ref(false);
const OpenDialog = () => {
    RowshiftList.value = [];
    BreedingCreate.breedId = 0;
    dialogVisible.value = true;
}
const BreedingCreate = reactive({
    "createBy": "string",
    "createDate": "2024-04-27T01:28:40.284Z",
    "updateBy": "string",
    "updateDate": "2024-04-27T01:28:40.284Z",
    "isDel": false,
    "breedId": 0,
    "earbugles": null,
    "serviceRam": null,
    "hybridizationType": null,
    "speciesEstrusType": null,
    "breedingScore": null,
    "currentBuilding": null,
    "currentField": null,
    "originallyDorm": null,
    "originallyColumns": null,
    "reproductiveState": null,
    "eventDate": null,
    "weight": null,
    "head": null,
    "breedingDate": null,
})
const HistoryInfo = reactive({
    createBy: "string",
    createDate: "2024-04-28T05:45:34.351Z",
    updateBy: "string",
    updateDate: "2024-04-28T05:45:34.351Z",
    isDel: false,
    recordId: 0,
    earbugles: null,
    breedingDate: null,
    seedMaleEarbugles: null,
    pregnancyCheckDate: null,
    pregnancyCheckResult: null,
    deliveryDate: null,
    healthyLambNumber: null,
    healthyLambWeight: null,
    weakLambNumber: null,
    deformityNumber: null,
    stillbirthNumber: null,
    mummyNumber: null,
    freaksNumber: null,
    atPresentParity: null,
    weaningDate: null,
    weaningNumber: null,
    weightOfWeaningLitter: null,
});
const BreedingAdd = () => {
    http.get("RouteRead/api/Historys/HistoryEarsel?EarNumber=" + BreedingCreate.earbugles).then(res => {
        var info = res.data;
        if (info.recordId != 0) {
            ElMessage.error("该羊只暂时不可配种，因为她的生产周期暂未结束")
            return;
        } else {
            if (!BreedingCreate.head) {
                ElMessage.error("请输入负责人")
                return;
            }
            if (!BreedingCreate.breedingDate) {
                ElMessage.error("请选择配种日期")
                return;
            }
            if (!BreedingCreate.earbugles) {
                ElMessage.error("请输入耳号")
                return;
            }
            if (!BreedingCreate.serviceRam) {
                ElMessage.error("请输入配种公羊耳号")
                return;
            }
            if (!BreedingCreate.hybridizationType) {
                ElMessage.error("请选择配种方式")
                return;
            }

            //更改羊只栋舍信息
            SheepUpd.earNumber = BreedingCreate.earbugles;
            SheepUpd.cottage = BreedingCreate.originallyDorm;
            SheepUpd.field = BreedingCreate.originallyColumns;
            http.put("Mroute/api/SheepFile/SheepFileConditionAuricleUpdate", SheepUpd)

            http.post("Kroute/api/Breedings/BreedingCreate", BreedingCreate).then(x => {
                if (x.data > 0) {
                    ElMessage.success("保存成功")
                    dialogVisible.value = false;
                    GetBreedingList();

                    HistoryInfo.earbugles = BreedingCreate.earbugles;//种母耳号
                    HistoryInfo.breedingDate = BreedingCreate.breedingDate;//配种日期
                    HistoryInfo.seedMaleEarbugles = BreedingCreate.serviceRam;//种公耳号
                    HistoryInfo.atPresentParity = info.atPresentParity + 1;//当前胎次
                    //添加种母历史记录
                    http.post("Kroute/api/Historys/HistoryCreate", HistoryInfo);

                    //更改档案的妊娠状态为已妊娠
                    http.get("MYroute/api/SheepFile/SheepFileConditionFinds?card=" + BreedingCreate.earbugles).then(res => {
                        var form = res.data;
                        form.reproductiveState = 7;
                        http.put("Mroute/api/SheepFile/SheepFileConditionUpdate", form)
                    })

                } else {
                    ElMessage.error("保存失败")
                }
            })
        }
    })
}
//羊只档案
//羊只档案表单
const form1 = reactive({
    "id": 0,
    "visualEarSignal": "",
    "electronicEarNumber": "",
    "breed": "",
    "sex": "",
    "birthday": "",
    "ageDays": "",
    "ageMoon": "",
    "presence": "",
    "growthStage": "",
    "sheepType": "",
    "cottage": "",
    "field": "",
    "reproductiveState": "",
    "pregnancyTest": true,
    "statusDays": "",
    "productionGrade": "",
    "salesGrade": "",
    "geneClass": "",
    "birthWeight": "",
    "weaningWeight": "",
    "weaningDate": "",
    "weaningAge": "",
    "fatherHorn": "",
    "femaleHorn": "",
    "earNumber": "",
    "placeBirth": "",
    "remark": "",
    "createBy": "",
    "createDate": "",
    "updateBy": "",
    "updateDate": "",
})




//修改页容量
const handleSizeChange = (val: any) => {
    page.pageSize = val;
    GetBreedingList();
}
//修改页序号
const handleCurrentChange = (val: any) => {
    page.pageIndex = val;
    GetBreedingList();
}
//获取配种信息
const search = ref({
    BreedingDate: [],
    Earbugles: "",
    Head: ""
})
const page = reactive({
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    pageCount: 0,
})
interface Breeding {
    "breedId": 5,
    "earbugles": "bvq8DWkqiZ",
    "serviceRam": "ioXV68S3C1",
    "hybridizationType": "Frz4spGG48",
    "speciesEstrusType": "ovnCPoyLJ5",
    "breedingScore": "oDwQBLAqoF",
    "currentBuilding": "LShYL01JbD",
    "currentField": "xFxp1o36QK",
    "originallyDorm": "5E0S7ysirO",
    "originallyColumns": "NX1DGTmqox",
    "reproductiveState": false,
    "eventDate": "2022-01-15T17:52:20",
    "weight": 525,
    "head": "mj8GmsTqdB",
    "breedingDate": "2024-03-24T04:57:23",
    createBy: "",
    createDate: "",
    updateBy: "",
    updateDate: "",
    isDel: false,
    //上一次配种日期
    lastDate: "",
    "breedinghenType": "",
    "seedmaleType": "",
}
const BreedingList = ref<Breeding[]>([]);
const GetBreedingList = () => {
    var dates = "";
    if (search.value.BreedingDate) {
        dates = search.value.BreedingDate.join(",");
    }
    http.get("RouteRead/api/Breedings/GetBreedingList", {
        params: {
            pageIndex: page.pageIndex,
            pageSize: page.pageSize,
            BreedingDate: dates,
            Earbugles: search.value.Earbugles,
            Head: search.value.Head,

        }
    }).then(x => {
        BreedingList.value = x.data.datas;
        page.totalCount = x.data.totalCount;
        page.pageCount = x.data.pageCount;
    })
}


//获取种公种母后裔耳标（分类后）
const ZMEarList = ref<{
    name: ""
}[]>([]);
const ZGEarList = ref<{
    name: ""
}[]>([]);
const HYEarList = ref<{
    name: ""
}[]>([]);
const GetEarList = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionarchives").then(x => {
        x.data.forEach((a: any) => {
            if (a.sheepType == "种母") {
                ZMEarList.value.push({
                    name: a.card
                })
            } else if (a.sheepType == "种公") {
                ZGEarList.value.push({
                    name: a.card
                })
            } else {
                HYEarList.value.push({
                    name: a.card
                })
            }
        });
    })
}

//修改羊只档案信息
var SheepUpd = reactive({
    earNumber: null,
    cottage: null,
    field: null,
})

onMounted(() => {
    GetBreedingList();
    GetEarNumber();
    GetCreateInDormList();
    GetHeadList();
    GetEarList();
})

</script>


<style></style>